<template>
<div class="register">
    <div class="header">
      <h1>找回密码</h1>
    </div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="registerForm">
        <el-form-item label="用户邮箱" prop="email">
            <el-input v-model="ruleForm.email"  placeholder="用户登录与找回密码用，请谨慎填写">
            <el-button slot="append">获取动态验证码</el-button>
            </el-input>
        </el-form-item>
        <el-form-item label="获取验证码" prop="checkCode1">
          <el-input placeholder="请输入6位验证码" v-model="ruleForm.checkCode">
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button ref="submit" type="primary" @click="submitForm('ruleForm')" :disabled="true">保存密码</el-button>
            <el-button @click="resetForm('ruleForm')">返回登录</el-button>
        </el-form-item>
    </el-form>
</div>  
</template>

<script>
export default {
 data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
            this.$refs.submit.disabled=false;
          callback();
        }
      };
      return {
        ruleForm: {
          name: '',
          emali: '',
          pass:'',
          checkPass:'',
          checkCode:'',
        },
        rules: {
          email :[
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
           ],
          pass: [
            { validator: validatePass, trigger: 'blur' },
            { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' },
            { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
          ],
          checkCode1:[
            { required: true, message: '验证码不能为空'},
            { type: 'number', message: '验证码必须为数字'}
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.$router.push('/login');

      }
    }
}
</script>

<style lang="less" scoped>
.register{
  position: relative;
  height: 100%;
  width: 100%;
  .registerForm{
      position: absolute;
      width: 60%;
      height: 100%;
      margin-top: 3rem;
      top: 15%;
      transform: translateX(-50%);
      left: 50%;
  }
}
.header{
  background-color: rgb(14, 156, 238);
  height: 15%;
  width: 100%;
  position: absolute;
  top: 0%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
//   justify-content: space-between;
}
</style>